import { getStream } from '../Core/connect'
import { Vector3 } from '../Math/Vector3'
import * as MathUtils from '../Math/MathUtils'

export class Object3D {
    readonly type: string
    name: string
    groupId?: string
    position: Vector3
    rotate: Vector3
    size: Vector3
    uuid: string
    // taskId:string
    // longitude:string
    // latitude:string
    // height:string
    constructor(option: Object3DOption) {
        this.type = option.type
        this.uuid = MathUtils.generateUUID()
        this.name = option.name
        this.position = option.position ? option.position : new Vector3(0, 0, 0)
        this.rotate = option.rotate ? option.rotate : new Vector3(0, 0, 0)
        this.size = option.size ? option.size : new Vector3(1, 1, 1)
        // this.taskId=option.taskId
        // this.longitude=option.longitude
        // this.latitude=option.latitude
        // this.height=option.height
        this.init()
    }

    init() {
        const stream = getStream()
        const str = { 
            function: 'createObject3D',
            type: `${this.type}`,
            ids: `${this.type},${this.name},${this.uuid}`, 
            position: `${this.position.x},${this.position.y},${this.position.z}`,
            rotate: `${this.rotate.x},${this.rotate.y},${this.rotate.z}`,
            size: `${this.size.x},${this.size.y},${this.size.z}`,
        }
        stream.emitUIInteraction(str)
    }

    static setAllVisibleByTag(id: string, visibleType: visibleType) {
	    const stream = getStream()
        const str = {function: 'setAllVisibleByTag', ids: id, show: visibleType}
        stream.emitUIInteraction(str)
	}

	setVisible(visibleType: visibleType) {
        const stream = getStream()
        const str = {function: 'setVisibleByUUID', ids: this.uuid, show: visibleType}
        stream.emitUIInteraction(str)
	}

    setGroupVisible(visibleType: visibleType) {
        if(this.groupId) {
            Object3D.setAllVisibleByTag(this.groupId, visibleType)
        }
    }

	setAllVisible(visibleType: visibleType) {
	    Object3D.setAllVisibleByTag(this.type, visibleType)
	}

	setGroupId(id: string) {
        this.groupId = id
        const stream = getStream()
        const str = {function: 'setLabel', ids: this.uuid, label: id}
        stream.emitUIInteraction(str)
	}

	// setVisibleByGroupId(visibleType: visibleType) {
    //     if(this.groupId) {
    //         //Object3D.setVisibleById(this.groupId, visibleType)
    //     }
	// }

	
    setPosition(position: Vector3) {
        this.position = position
        const stream = getStream()
        const str = {function: 'setPosition', ids: `${this.uuid}`, position: `${this.position.x},${this.position.y},${this.position.z}`}
        stream.emitUIInteraction(str)
    }

    setLonLatPosition(position: Vector3){
        const stream = getStream()
        const getNewPositon = (data: string) => {
            const returnData = JSON.parse(data)
            if (returnData.ids === this.uuid) { 
                this.position = new Vector3(returnData.position.x, returnData.position.y, returnData.position.z)
                stream.removeResponseEventListener("getNewPositon")
            }
        }
        stream.addResponseEventListener("getNewPositon", getNewPositon)
        
        const str = {function: 'setLonLatPosition', ids: `${this.uuid}`,position:`${position.x},${position.y},${position.z}`}
        stream.emitUIInteraction(str)
        
    }

    setRotate(rotate: Vector3) {
        this.rotate = rotate
        const stream = getStream()
        const str = {function: 'setRotate', ids: `${this.uuid}`, rotate: `${this.rotate.x},${this.rotate.y},${this.rotate.z}`}
        stream.emitUIInteraction(str)
    }

    setSize(size: Vector3) {
        this.size = size
        const stream = getStream()
        const str = {function: 'setSize', ids: `${this.uuid}`, size: `${this.size.x},${this.size.y},${this.size.z}`}
        stream.emitUIInteraction(str)
    }

    getMatrix() {
        const uuid = this.uuid
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { function: 'getObjectMatrix', sessionId: sessionId, ids: `${uuid}` }
            stream.emitUIInteraction(message)
            const getMatrixReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) { 
                    resolve(returnData)
                    stream.removeResponseEventListener("getObjectMatrix")
                }
            }
            stream.addResponseEventListener("getObjectMatrix", getMatrixReady)
        })
	}

	delete() {
        const stream = getStream()
        const str = {function: 'deleteObject3D', ids: `${this.uuid}`}
        stream.emitUIInteraction(str)
	}

    remove() {
        if(this.groupId) {
            const stream = getStream()
            const str = {function: 'deleteObject3D', ids: `${this.groupId}`}
            stream.emitUIInteraction(str)
        }
    }

    clear() {
        const stream = getStream()
        const str = {function: 'deleteObject3D', ids: `${this.type}`}
        stream.emitUIInteraction(str)
	}
//     createwrj(){
//         const stream = getStream()
//         const str={function:'createwrj',createwrjfunction:'getTaskWorkID',taskId:`${this.taskId}`}
//          stream.emitUIInteraction(str)
//     }
//     createflightactor(){
//         const stream=getStream()
//         const str={function:'createflightactor',taskId:`${this.taskId}`}
//         stream.emitUIInteraction(str)
//     }
//     setRealPosition(){
//         const stream=getStream()
//         const str={function:'setRealPosition',longitude:`${this.longitude}`,latitude:`${this.latitude}`,height:`${this.height}`}
//         stream.emitUIInteraction(str)
//     }
 }